package com.egreen.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.egreen.common.pojo.DTO.system.SystemInformationDTO;
import com.egreen.common.pojo.VO.system.GetSystemInformationVO;
import com.egreen.common.pojo.entity.system.SystemInformation;

import com.egreen.system.dao.SystemInformationDao;
import com.egreen.system.ex.SystemException;
import com.egreen.system.service.ISystemInformationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author cloudJiang
 * @since 2022-07-16
 */
@Service
//@Log4j
public class SystemInformationServiceImpl extends ServiceImpl<SystemInformationDao, SystemInformation> implements ISystemInformationService {
    @Autowired
    SystemInformationDao systemInformationDao;

    /**
     * 用户查看自己的  系统信息
     *
     * @return
     */

    @Override
    public List<GetSystemInformationVO> getSystemInformation(int userId) {

        QueryWrapper wrapper = new QueryWrapper();
        //查询的内容
        wrapper.select("id", "send_time", "title", "content", "state");
        //查询条件
        wrapper.eq("user_id", userId);
        List list = systemInformationDao.selectList(wrapper);

        return list;
    }

    /**
     * 添加系统信息
     */
    @Override
    public void addSystemInformation(SystemInformationDTO systemInformationDTO) {
        SystemInformation systemInformation = new SystemInformation();
        systemInformation.setContent(systemInformationDTO.getContent());
        systemInformation.setAdminId(systemInformationDTO.getAdminId());
        systemInformation.setTitle(systemInformationDTO.getTitle());
        systemInformation.setUserId(systemInformationDTO.getUserId());

        int insert = systemInformationDao.insert(systemInformation);
        System.out.println(insert);
    }

    /**
     * 删除系统信息
     */
    @Override
    public void deleteSystemInformation(int id) {
        //删除该条信息
        int i = systemInformationDao.deleteById(id);

        if (i != 1) {
            //删除失败
            throw new SystemException("删除失败");
        }
    }

}
